package cn.kilo.domain.support;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author kilok
 */
@Component
@Data
@Slf4j
@Configuration
public class WeChatPayConfig {
    /**
     * 商户号
     */
    private final String MCH_ID = "1677148696";

    /**
     * 公众号id 需要和商户号绑定
     */
    private final String WX_PAY_APPID = "wx71df0eacad878acf";
    /**
     * 商户证书序列号,需要和证书对应
     */
    private final String MCH_SERIAL_NO = "1204C31EC5F3E4248253BB2FC7D5D548AA3BDE9B";
    /**
     * API V3密钥
     */
    private final String API_V3_KEY = "7TYWfF5idjRMzGkqNNbhIwbJnZABXzqa";
    /**
     * 商户私钥路径（微信服务端会根据证书序列号，找到证书获取公钥进行解密数据）
     */
    private final String privateKeyPath = "-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCuCjjKDxh4DTuf\n" +
            "bgzYPYTvcvcsp6LPelZT4hA+Hb3v7k/v/z1yqTQxXmnPOX2eB6AkOO1fX4Yaoi79\n" +
            "TcvnpeVk4pBPZwbe38swwlwRx95di//sATEUyZvxQzX5Lgc1ACjFvnQMcFZVQkWC\n" +
            "Buu9Rku2CF51vBSDC+lVB2X52pdugqmoHSCmI6oiGA/6ylE7Vlrbd3yNBqKR9VFh\n" +
            "vSQxmKiZWZte0Gb2aVDWbz5UiqHlXQAi1jPVubCahmpuEWW6qmHcuTIgn/jagFtV\n" +
            "rYlAReqxqSr3HQvHhb4xEH+4I2gIN8a+Pjhut5j8tGQyS78pFoXJDGboDFSGatz1\n" +
            "DnhpOsjJAgMBAAECggEAJjgocHcQMIz3YvOP7ozT/KGKlkvZ/SYofxNN7dYqN95l\n" +
            "v5aVr/Bofc71/YPqiiGykUHPHvqjQIYNO6Mx/BT6Y0U927IMWDVdMb/86Aq0UmbE\n" +
            "NNeuJ5p+nGTeHXJ2MrVuPr6sTW7+BosfKIbMjiw5xCfiVoiPGJgtDXcqdcwDJUk/\n" +
            "m2S6fsCLWyQTAmED8I8TvVckM+gsstdY10GfjMaAWd6u/Q3TfXt/gl/1Y3t0itr7\n" +
            "8ucudSJ1aQnh72lLtx5hmhSJsteakvkHTIJqNy3EhWhDJ8x60hGlHMC+O0fIDY09\n" +
            "6kqaT8+18VjwUH56GIJ5xfY3KfLv8vsDnp7CDS+IAQKBgQDczejzgqnS6wda46l+\n" +
            "HVn/T5gS81DmfZLH9c6tsksgPhQNCmOpkpuVDBQmP/ByG0LmoLoTIFcN6tS/hFJn\n" +
            "OIMirFx9xFPCEsk4e/KrxNKPdufWifHLw0pZ2pGMHzl05F2yGg+vApUQQbNPE5Ni\n" +
            "MnmvLTOl0+QFPKdzApHRX710YQKBgQDJyA3vUWKbquicf+7DuHinB5+iUwz0YJYg\n" +
            "bZhkT6qmF0TURokwvMKLlpFQp7oPzT2yPC8sBA/FWHnVJ04X+RRzQ1I/06pSMs3d\n" +
            "Kqs8FZAtfow9zKjbAikMtXLx9d83xtW/+vnyHPq6zT0apO6IqvgntnUNWWu6DRcc\n" +
            "pgdiYLEtaQKBgC5CLUWm4DFB7/6Ip5+2Qn6MjE2cEBzZvgCKbMxTCA4vM0AwJwUF\n" +
            "o/ar2yTQTG17bJ6omZMcOWC/C+c7r4/UdRS0i4QFt453JRTiUsrNp1dLD/MQX0nT\n" +
            "FiaQ9eEnQYBNv5tqflKp9dS8DvbIIR53eKrMi394TtMSC/q6VRZKOoTBAoGADZsK\n" +
            "rX9cWci9ZIgPzmyq3gBamDRnZDb7CHa7VtA0XnylLHcRGxMvPwBJDhoxJo2dQl5i\n" +
            "g+JGOfVMSb4buuwVn+wDKMCZ9G7uiKIu559AmIC27hfVrubHY878UCpXAcjLcuV2\n" +
            "YwfnNB9QpnJGjzqP/wWDgQHtjJ8DVvAvAUgpyjkCgYB8hG/rCHTGoL9O/vmHek0M\n" +
            "1Xqc/JvkiUW3Cdw1wx9BGxmpx7/56jTpqGryL3PL1YNyGgVXAL7GjMOkjAThM5O1\n" +
            "ozjeQJ9lMkJzQlriltKWIfd0UlRwDv7pXnbcbwauaO/AHs/lPuU3p4ZfVISpX/ZL\n" +
            "aeNH6e0HRyT0ies2JotetA==\n" +
            "-----END PRIVATE KEY-----\n" +
            "\n";
    /**
     * 支付成功页面跳转
     */
    private String successReturnUrl;

    /**
     * 支付成功，回调通知
     */
    private String callbackUrl;

    /**
     * 微信支付v3-url
     */
    private final String baseUrl="https://api.mch.weixin.qq.com/v3";


//    /**
//     * 获取商户的私钥文件
//     * @param key
//     * @return
//     */
//    public PrivateKey getPrivateKey(String key){
//
////        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream(keyPath);
////        if(null==inputStream){
////            throw new RuntimeException("私钥文件不存在");
////        }
//        return PemUtil.loadPrivateKey(key);
//    }
//
//    /**
//     * 获取证书管理实例
//     *
//     * @return
//     */
//    @Bean
//    public Verifier getVerifier()throws GeneralSecurityException, IOException, HttpCodeException, NotFoundException {
//
//        log.info("key:{}",privateKeyPath);
//        //获取商户私钥
//        PrivateKey privateKey=getPrivateKey(privateKeyPath);
//
//
//        //私钥签名对象
//        PrivateKeySigner privateKeySigner=new PrivateKeySigner(mchSerialNo,privateKey);
//
//        //身份认证对象
//        WechatPay2Credentials wechatPay2Credentials=new WechatPay2Credentials(mchId,privateKeySigner);
//
//        //使用定时更新的签名验证器,不需要传入证书
//        CertificatesManager certificatesManager=CertificatesManager.getInstance();
//        certificatesManager.putMerchant(mchId,wechatPay2Credentials,apiV3Key.getBytes(StandardCharsets.UTF_8));
//
//        return certificatesManager.getVerifier(mchId);
//    }

    @Bean(name = "closeableHttpClient")
    public CloseableHttpClient httpClient() {
        return HttpClientBuilder.create().build();
    }
    /**
     * 获取支付http请求对象
     * @param verifier
     * @return
     */
//    public CloseableHttpClient getWechatPayClient(Verifier verifier){
//        PrivateKey privateKey=getPrivateKey(privateKeyPath);
//
//        WechatPayHttpClientBuilder builder=WechatPayHttpClientBuilder.create()
//                .withMerchant(mchId,mchSerialNo,privateKey)
//                .withValidator(new WechatPay2Validator(verifier));
//        // 通过WechatPayHttpClientBuilder构造的HttpClient，会自动的处理签名和验签，并进行证书自动更新
//        return builder.build();
//    }
//
//    /**
//     * 获取httpClient 无需进行签名验证，跳过验签的过程
//     * @return
//     */
//    public CloseableHttpClient getWechatPayNoSignClient(){
//
//        PrivateKey privateKey=getPrivateKey(privateKeyPath);
//
//        //构造httpClient
//        WechatPayHttpClientBuilder builder=WechatPayHttpClientBuilder.create()
//                //设置商户信息
//                .withMerchant(mchId,mchSerialNo,privateKey)
//                //无需进行签名验证、通过withValidator((response) -> true)实现
//                .withValidator((response)->true);
//
//        // 通过WechatPayHttpClientBuilder构造的HttpClient，会自动的处理签名和验签，并进行证书自动更新
//        return builder.build();
//    }

}
